package com.example.demospringbootwebservice.configuration;

import com.example.demospringbootwebservice.test.TestWebService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfiguration {
    @Resource
    TestWebService testWebService;

    @Bean(name = "cxfServlet")
    public ServletRegistrationBean<CXFServlet> cxfServlet() {
        return new ServletRegistrationBean<>(new CXFServlet(), "/ws/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 多个 WebService就多复制几分
     */
    @Bean
    public Endpoint endpoint_testWebService() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), testWebService);
        // 这里最好提取到配置文件中
        endpoint.setPublishedEndpointUrl("http://localhost:8080/ws/testWebService");
        endpoint.publish("/testWebService");
        return endpoint;
    }
}
